package com.librarysys.controller;

import com.librarysys.common.Const;
import com.librarysys.common.Result;
import com.librarysys.dao.PurviewMapper;
import com.librarysys.entity.Manager;
import com.librarysys.entity.Purview;
import com.librarysys.entity.Reader;
import com.librarysys.entity.Readertype;
import com.librarysys.service.IReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by yuhui on 2017/7/12.
 */
@Controller
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    private IReaderService iReaderService;

    @Autowired
    private PurviewMapper purviewMapper;

    @RequestMapping("/type/list")
    public String readerTypeList(HttpServletRequest request){
        Boolean comfirm = comfirm(request.getSession());
        if(!comfirm){
            return "no_access";
        }
        Result result = iReaderService.listReaderType();
        request.setAttribute("types",result.getData());
        return "readerType";
    }

    @RequestMapping("/type/get_all")
    @ResponseBody
    public List<Readertype> readerTypeGetAll(HttpServletRequest request){
        Result result = iReaderService.listReaderType();
        List<Readertype> list=(List<Readertype>) result.getData();
        return list;
    }

    @RequestMapping("/type/add")
    public String addReaderType(Readertype readertype,HttpServletRequest request){
        Boolean comfirm = comfirm(request.getSession());
        if(!comfirm){
            return "no_access";
        }else{
            Result result = iReaderService.addReaderType(readertype);
            request.setAttribute("msg",result.getMsg());
            return "/reader/type/list";
        }

    }

    @RequestMapping("/type/del")
    public String delReaderType(Integer id,HttpServletRequest request){
        Boolean comfirm = comfirm(request.getSession());
        if(!comfirm){
            return "no_access";
        }else{
            Result result = iReaderService.delReaderType(id);
            request.setAttribute("msg",result.getMsg());
            return "/reader/type/list";
        }

    }

    @RequestMapping("/list")
    public String readerList(HttpServletRequest request){
        Boolean comfirm = comfirm(request.getSession());
        if(!comfirm){
            return "no_access";
        }
        Result result = iReaderService.listReader();
        request.setAttribute("readers",result.getData());
        return "reader";
    }

    @RequestMapping("/add")
    public String addReader(Reader  reader,String birth,String createDate, HttpServletRequest request){
        Boolean comfirm = comfirm(request.getSession());
        if(!comfirm){
            return "no_access";
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = sdf.parse(birth);
                Date parse1 = sdf.parse(createDate);
                reader.setBirthday(parse);
                reader.setCreatedate(parse1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Result result = iReaderService.addReader(reader);
            request.setAttribute("msg",result.getMsg());
            return "/reader/list";
        }

    }

    @RequestMapping("/del")
    public String delReader(Integer id,HttpServletRequest request){
        Boolean comfirm = comfirm(request.getSession());
        if(!comfirm){
            return "no_access";
        }else{
            Result result = iReaderService.delReader(id);
            request.setAttribute("msg",result.getMsg());
            return "/reader/list";
        }

    }



    private Boolean comfirm(HttpSession session){
        Manager manager = (Manager) session.getAttribute(Const.CURRENT_USER);
        Purview purview = purviewMapper.selectByPrimaryKey(manager.getId());
        if(purview==null){
            return false;
        }
        if(purview.getReaderset()){
            return true;
        }
        return false;
    }
}
